package com.dk.service.impl;

import com.dk.api.PartCodeAPI;
import com.dk.api.RuleCodeAPI;
import com.dk.code.CodeFectory;
import com.dk.em.Result;
import com.dk.pojo.SysAutoCodePart;
import com.dk.pojo.SysAutoCodeRule;
import com.dk.pojo.SysAutoCodeRuleExample;
import com.dk.service.generateCodeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class generateCodeServiceImpl implements generateCodeService {

    //Fegin调接口
    @Autowired
    private RuleCodeAPI ruleCodeAPI;

    @Autowired
    private PartCodeAPI partCodeAPI;

    @Override
    public String selectCode(String codename) {
        List<SysAutoCodeRule> sysAutoCodeRules = ruleCodeAPI.selectByExample(codename);//远程调用接口mapper
        Long ruleId=null;
        String str="";
        //1.判断WAREHOUSE_CODE是否存在  存在  首先根据传过来的规则查询这条数据    规则rulecode是唯一的  所以不考虑多条重复的问题
        if (sysAutoCodeRules !=null && sysAutoCodeRules.size()>0){
            SysAutoCodeRule sysAutoCodeRule = sysAutoCodeRules.get(0);
            //2.根据规则表中的id查询规则组成表
            ruleId = sysAutoCodeRule.getRuleId();
            List<SysAutoCodePart> partList = partCodeAPI.selectByRuleId(ruleId);//根据id查询类型
            if (partList != null && partList.size()>0){
                if (partList.size()<2){
                    return "规则不能小于两条否则无法生成";//无法拼接
                }
            }
            //3.根据规则组成表中的类型选择不同的算法生成不同的数据   sysAutoCodePart.get(1).getPartType()
            String partType1 = partList.get(0).getPartType();
            String partType2 = partList.get(1).getPartType();
            String code = CodeFectory.map.get(partType1).code(partType1, ruleId);
            String code1 = CodeFectory.map.get(partType2).code(partType2, ruleId);
            str+=code+code1;
            System.out.println(str);
            //4.合成两个字符串  返回即可
        }
        return str;
    }
}
